home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 4 / MacMania 4.toast / / Tools&Utilities / MathPad 2.4 / Documentation / Programming < prev   
Text File  |  1994-06-20  |  6KB  |  122 lines

  1. ---------------------- Programming in MathPad
  2.  
  3. -- The basic MathPad language is intended to be mathematical rather than procedural. Equations can be written and evaluated in a natural way that does not really require any programming. There are however some handy numerical algorithms that do require some procedural operations.
  4.  
  5. -- MathPad does not have all the handy features of a programming language like C or Pascal but it does have all the essential elements needed to do programming.
  6.  
  7. -- The assignment operator.
  8. -- It is very important to understand the difference between = and :=. The = statement gives a definition and does not perform any calculation. The = statement is analogous to a function definition in C or Pascal.
  9.  
  10. -- MathPad makes two passes over the text. The first pass saves all the definitions. The second pass evaluates statements in order.
  11.  
  12.   a:7.00;    -- look at definition of "a" and perform calculation.
  13.   a = b+2    -- define "a" (on pass 1)
  14.   b = 5      -- define "b" (on pass 1)
  15.  
  16. -- The := operator replaces any definition of the left hand variable with the calculated value of the right hand side.
  17.  
  18.   c:?;       -- there is no definition for "c"
  19.   c:= b+2:;  -- perform calculation. save result in "c"
  20.   c:7.00;    -- access the saved value. no calculation performed
  21.   b:=10:;    -- change value of "b"
  22.   a:12.00;   -- calculate "a" using new value of "b"
  23.   c:7.00;    -- value of "c" does not change
  24.  
  25. -- Normally the destination of := is a symbol that does not have any = definition. It is sometimes useful to have an = definition calculate an initial value but remember that once := is used the definition will be erased.
  26. -- Since := is sequential and = is not, it is possible (but not recommended) to write things that look rather confusing.
  27.  
  28.   d:=d+1:
  29.   d = 3
  30.   d:4.00
  31.  
  32. -- Entire arrays can be assigned if they are finite (and there is enough memory). Individual array elements can be assigned in limited cases. A single numeric value can be assigned to an element of an array that has previously been initialized by assignment.
  33.  
  34.  A := {{1,2,3,4},{5,6,7,8}}:
  35.  A[1,2] := 0:
  36.  A:{{1.00,0.00,3.00,4.00},{5.00,6.00,7.00,8.00}}
  37.  
  38. -- Some symbols are not allowed as := destinations. Array definitions with explicit indexes can not be assigned to. Also special symbols such as the plot variable X or sum() variables can not be assigned to.
  39.  
  40.    B[i]=i
  41. -- B:=3:   -- is not allowed
  42.  
  43.  
  44. ---------- The comma operator
  45. -- MathPad's functional approach deals with expressions and does not use blocks containing sequences of statements. However, the comma operator can be used to perform a sequence of operations. Parenthesis group the sequence into a single expression.
  46. -- The first purpose of the comma operator is to separate conditional "when" clauses. The result will be the value corresponding to first condition in the list that evaluates true.
  47.  
  48.   select(s) = a when s=1, b when s=2, c when s=3
  49.   select(1):12.00;  select(2):10.00;
  50.  
  51. -- The meaning of the comma operator is extended to allow any expression (not just when clauses). The result will be the first item in the list with a true "when" clause or the first expression that evaluates to a known value. Evaluation of an assignment performs the side effect only and does not result in a known value. This allows a sequence of assignments to be done simply by separating them with commas.
  52.  
  53.   b:=b+1, c:=b, a:13.00
  54.   b:11.00;  c:11.00
  55.  
  56. -- The double comma operator
  57. -- In cases involving assignments it is sometimes desirable to have evaluation of a sequence continue even if a true when clause is encountered. For the double comma, each expression is evaluated regardless of when clauses or known results. The result of a double comma list is the last expression in the list. Single and double commas can be mixed in the same list.
  58.  
  59.   absb(x) = b:=x,
  60.            b:=-b when b<0,,  -- continue even if b<0
  61.            b                 -- return value of b
  62.  
  63. ---------- Iteration
  64. -- The 'while' operator can be used to evaluate an expression repeatedly. The while keyword follows the expression but the condition is checked before each evaluation.
  65.  
  66.   factorial(n) = i:=1,
  67.                  m:=1,  -- set up before starting loop
  68.                  (i:=i+1,m:=m*i) while i<n, -- does nothing if n<=1
  69.                  m      -- return final value for m
  70.  
  71.   factorial(1):1.00;  i:1.00;  m:1.00
  72.   factorial(2):2.00;  i:2.00;  m:2.00
  73.  
  74. -- Nested while loops are fine but remember that all := variables are global so don't try using 'i' as the index for both an inner and outer loop.
  75.  
  76. -- sum(), plot and table can also be used to perform iteration. The following examples all evaluate dostuff() n times while stepping its parameter from 1 to n. dostuff() can be any expression and can contain anything including :=.
  77.  
  78.   dostuff(x) = (ct:=1,1) when x=1,
  79.                ct:=ct+x, ct
  80.   n=5
  81.  
  82.   sum(dostuff(x),x,1,n):35.00;  -- Note: sum() will stop on unknown
  83.  
  84.   Xmin=1; Xmax=n; Xsteps=n
  85.   plot dostuff(X)
  86.  
  87.   Nmin=1; Nmax=n; Nsteps=n
  88.   table dostuff(N)
  89.        1.00
  90.        3.00
  91.        6.00
  92.       10.00
  93.       15.00
  94.  
  95. -- Both plot and table evaluate their expression extra times at the 1st value in order to determine array dimensions. To work properly dostuff() must reset its initial state based on its parameter.
  96. -- Using plot with expressions that have side effects can be tricky. If the plot expression is an {x,y} array it gets evaluated twice per plotted point (once for the x coord, once for the y coord). It is best to generate values based on the independent variable rather than based on only on previous values.
  97.  
  98.  
  99. ---------- Debugging
  100. -- It may be helpful to create a debugging function to save values during evaluation. The following routines will save a sequence of up to nmax values in the array dbgdata.
  101.  
  102.   startdbg(nmax) = dbgdata:= 0/0[:nmax],-- initialize array
  103.                    ndbg:=0
  104.  
  105.   dbg(val) = ndbg:=ndbg+1,
  106.              dbgdata[ndbg]:=val 
  107.  
  108.  
  109. -- try looking at the values of the "ct" global during dostuff() summation.
  110.  
  111.   startdbg(10):
  112.   sum((dbg(ct),dostuff(x)),x,1,n):35.00;
  113.  
  114.   ndbg:5.00;      -- 5 values were saved
  115.  
  116.   table dbgdata[:ndbg]
  117.       15.00
  118.        1.00
  119.        3.00
  120.        6.00
  121.       10.00
  122.